home *** CD-ROM | disk | FTP | other *** search
/ Mac Power 1997 December / MACPOWER-1997-12.ISO.7z / MACPOWER-1997-12.ISO / AMUG / PROGRAMMING / Raven 1.2.sit / Raven 1.2 / Source / Foundation / Common / ZInvariant.cpp < prev    next >
Text File  |  1997-06-18  |  2KB  |  93 lines

  1. /*
  2.  *  File:       ZInvariant.cpp
  3.  *  Summary:    Mixin for objects that use PRECONDITION and POSTCONDITION macros.
  4.  *  Written by: Jesse Jones
  5.  *
  6.  *  Copyright ゥ 1997 Jesse Jones. 
  7.  *    For conditions of distribution and use, see copyright notice in ZTypes.h  
  8.  *
  9.  *  Change History (most recent first):    
  10.  *
  11.  *         <1>     6/14/97    JDJ        Created.
  12.  */
  13.  
  14. #include <ZInvariant.h>
  15.  
  16. #include <ZDebug.h>
  17.  
  18.  
  19. #if DEBUG
  20.  
  21. // ===================================================================================
  22. //    class ZCheckInvariant
  23. // ===================================================================================
  24.  
  25. //---------------------------------------------------------------
  26. //
  27. // ZCheckInvariant::~ZCheckInvariant
  28. //
  29. //---------------------------------------------------------------
  30. ZCheckInvariant::~ZCheckInvariant()        
  31. {
  32.     if (mObject->mNesting == 1)
  33.         mObject->Invariant();
  34.  
  35.     mObject->mNesting--;
  36. }
  37.  
  38.  
  39. //---------------------------------------------------------------
  40. //
  41. // ZCheckInvariant::ZCheckInvariant
  42. //
  43. //---------------------------------------------------------------
  44. ZCheckInvariant::ZCheckInvariant(const MInvariant* object)        
  45. {
  46.     mObject = object; 
  47.     
  48.     mObject->mNesting++;
  49.         
  50.     if (mObject->mNesting == 1)
  51.         mObject->Invariant();
  52. }
  53.  
  54. #pragma mark -
  55.  
  56. // ===================================================================================
  57. //    class MInvariant
  58. // ===================================================================================
  59.  
  60. //---------------------------------------------------------------
  61. //
  62. // MInvariant::~MInvariant
  63. //
  64. //---------------------------------------------------------------
  65. MInvariant::~MInvariant()            
  66. {
  67. }
  68.  
  69.  
  70. //---------------------------------------------------------------
  71. //
  72. // MInvariant::MInvariant
  73. //
  74. //---------------------------------------------------------------
  75. MInvariant::MInvariant()
  76. {
  77.     mNesting = 0;
  78. }
  79.  
  80.  
  81. //---------------------------------------------------------------
  82. //
  83. // MInvariant::Invariant
  84. //
  85. //---------------------------------------------------------------
  86. void MInvariant::Invariant() const
  87. {
  88.     ASSERT(this != nil);                // should never fire (if it does the vtable will be garbage amd how did we get here?)
  89.     ASSERT((long) this % 4 == 0);        // should never fire
  90.     ASSERT(mNesting == 1);                // possibly too strict: prevents people from calling Invariant directly
  91. }
  92.  
  93. #endif    // DEBUG